package cn.yunhe.controller;

import cn.yunhe.api.OrderApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Auther: als
 * @Date: 2024/6/19 - 06 - 19 - 15:16
 * @Description: cn.yunhe.controller
 * @version: 1.0
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Autowired
    OrderApi orderApi;
    @GetMapping("/getOrder")
    public String getOrder(){
        ServiceInstance instance = loadBalancerClient.choose("order-server");
        RestTemplate restTemplate = new RestTemplate();
        String url = instance.getUri()+"/order/getOrder";
        String orderStr = restTemplate.getForObject(url, String.class);
        return "Consumer::"+orderStr;
    }

    @GetMapping("/getOrder2")
    public String getOrder2(){
        return "Consumer::"+orderApi.getOrder();
    }
}